using System.Reflection;
using Synapse.Framework.AoP.Attributes.Core;

namespace Synapse.Framework.AoP
{
    internal class ExecuteBehaviour : IExecuteBehaviour
    {
        private readonly object instance;

        public ExecuteBehaviour(object instance)
        {
            this.instance = instance;
        }

        public void BeforeCallMethod(MethodInfo methodInfo, object[] values)
        {
            var callMethodAttributes = methodInfo.GetCustomAttributes(typeof(BeforeCallMethodAttribute), true);
            foreach (BeforeCallMethodAttribute callMethodAttribute in callMethodAttributes)
                callMethodAttribute.Execute(new BeforeInvocationMethod(instance, methodInfo, values));

        }

        public void AfterCallMethod(MethodInfo methodInfo, object[] values, object returnValue)
        {
            var callMethodAttributes = methodInfo.GetCustomAttributes(typeof(AfterCallMethodAttribute), true);
            foreach (AfterCallMethodAttribute callMethodAttribute in callMethodAttributes)
                callMethodAttribute.Execute(new AfterInvocationMethod(instance, methodInfo, values, returnValue));

        }
    }
}